package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 根据id查询套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> byId = setmealDishMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(byId);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDtO
     */
    @Override
    public void update(SetmealDTO setmealDtO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDtO,setmeal);

        //修改套餐数据
        setmealMapper.update(setmeal);
        List<Long> lo = new ArrayList<>();
        lo.add(setmealDtO.getId());

        //先删除口味数据
        setmealDishMapper.deleteBySetmealId(lo);
        //重新插入口味数据

        List<SetmealDish> setmealDish = setmealDtO.getSetmealDishes();

        if(setmealDish!=null && setmealDish.size()>0){
            setmealDish.forEach(setmeals -> {
                setmeals.setSetmealId(setmealDtO.getId());
            });
            //插入口味数据
            setmealDishMapper.insertSetmealDishes(setmealDish);
        }
    }

    @Override
    public void add(SetmealDTO setmealDtO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDtO,setmeal);
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish>setmealDishes=setmealDtO.getSetmealDishes();
        if(setmealDishes!=null && setmealDishes.size()>0){
            setmealDishes.forEach(sd->{
                sd.setSetmealId(id);
            });
            setmealDishMapper.insert(setmealDishes);
        }
    }

    @Override
    public void status(Integer status, Long id) {
        //判断套餐内菜品起售状态
        if(status==StatusConstant.ENABLE){
            List<SetmealDish> byId = setmealDishMapper.getById(id);
            for (SetmealDish sd : byId) {
                Dish byId1 = dishMapper.getById(sd.getDishId());
                if(byId1.getStatus()==StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        //构建更新状态对象
        Setmeal build = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(build);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        
        setmealMapper.deleteById(ids);
        setmealDishMapper.deleteBySetmealId(ids);  //删除套餐时对应套餐内菜品删除
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
